package com.jumi.microservice.service.api;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jm.member.common.api.web.R;
import com.jumi.microservice.common.exception.BaseException;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.constant.comment.CommentEnum;
import com.jumi.microservice.controller.api.SensitiveWordsUtil;
import com.jumi.microservice.domain.GoodsSkuInfoDTO;
import com.jumi.microservice.dto.comment.api.*;
import com.jumi.microservice.dto.reponse.JmUserReponse;
import com.jumi.microservice.dto.reponse.UserExternalResponse;
import com.jumi.microservice.entity.Comment;
import com.jumi.microservice.entity.SpecialKeyword;

import com.jumi.microservice.mapper.CommentMapper;
import com.jumi.microservice.mapper.SpecialKeywordMapper;
import com.jumi.microservice.service.CommodityApi;
import com.jumi.microservice.service.IMemberService;
import com.jumi.microservice.service.IUserExternalService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;


/**
 * @author Scw
 * @Date 2020-08-04 17:57
 */
@Service
public class CommentService {
    private static final Logger log = LoggerFactory.getLogger(CommentService.class);
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private SpecialKeywordMapper specialKeywordMapper;
    /*引入商品接口*/
    @DubboReference
    private CommodityApi goodsClient;

    @DubboReference
    private IUserExternalService iUserExternalService;
    @DubboReference
    private IMemberService iMemberService;

    /**
     * 评论添加
     *
     * @param commentApiAddRequest
     * @return
     */
    public Boolean addComment(CommentApiAddRequest commentApiAddRequest) {
        //获取当前用户ID
        Long userId = commentApiAddRequest.getUid();
        if (userId == null) {
            throw new BaseException(CommentEnum.NO_LOGIN.getCode(), CommentEnum.NO_LOGIN.getMessage());
        }
        Integer gid = commentApiAddRequest.getGid();
        if (gid == null) {
            throw new BaseException(CommentEnum.COMMENT_GId.getCode(), CommentEnum.COMMENT_GId.getMessage());
        }
        Integer spuGid1 = commentApiAddRequest.getSpuGid();
        if (spuGid1 == null || spuGid1.equals("")) {
            throw new BaseException(CommentEnum.COMMENT_SPUGID.getCode(), CommentEnum.COMMENT_SPUGID.getMessage());
        }
        //查询信息
        String content = commentApiAddRequest.getContent().trim();
        //字数限制
        if (content.length() > 200) {
            throw new BaseException(CommentEnum.COMMENT_LENGTH_LONG.getCode(), CommentEnum.COMMENT_LENGTH_LONG.getMessage());
        }
        if (content == null) {
            commentApiAddRequest.setContent("用户默认好评！");
        }
        commentApiAddRequest.setContent(content);

        String images = commentApiAddRequest.getImages();
        if (images != null) {
            //图片以；分割
            String[] idArr = images.split(";");
            if (idArr.length > 6) {
                throw new BaseException(CommentEnum.COMMENT_IMAGES_LONG.getCode(), CommentEnum.COMMENT_IMAGES_LONG.getMessage());
            }
            if (idArr.length > 0) {
                commentApiAddRequest.setIsImages((byte) 1);
            }
        }
        /*查询spugid  uid是否有数据如果有设置为1 否则为0，并把已有数据更新1*/
        QueryWrapper<Comment> spuWrapper = new QueryWrapper<>();
        spuWrapper.eq("spu_gid", spuGid1);
        spuWrapper.eq("uid", userId);
        spuWrapper.eq("is_hide", 0);
        Integer commentsList = commentMapper.selectCount(spuWrapper);
        log.info("评论添加查询日志:[{}]", JSON.toJSONString(commentsList));
        if (commentsList > 0) {
            //回头客
            commentApiAddRequest.setIsRepeatCustomers((byte) 1);
            Integer integer = commentMapper.repeatUpdata(spuGid1);
            if (integer == 0) {
                throw new BaseException(CommentEnum.COMMENT_REPEAT_ERROR.getCode(), CommentEnum.COMMENT_REPEAT_ERROR.getMessage());
            }
        }
        //entity
        Comment comments = new Comment();
        BeanUtils.copyProperties(commentApiAddRequest, comments);
        //拼接上uid
        comments.setUid(userId);
        return commentMapper.insert(comments) > 0;
    }

    /**
     * 评论列表
     *
     * @param commentApiListRequest
     * @return
     */
    public TableDataInfo<CommentApiListResponse> commentsApiList(CommentApiListRequest commentApiListRequest) {
        //获取当前用户ID
        Long userId = commentApiListRequest.getUid();
        if (userId == null) {
            throw new BaseException(CommentEnum.NO_LOGIN.getCode(), CommentEnum.NO_LOGIN.getMessage());
        }
        /**
         * 敏感词
         */
        QueryWrapper<SpecialKeyword> specialWrapper = new QueryWrapper<>();
        specialWrapper.orderByDesc("id");
        List<SpecialKeyword> specialKeywords = specialKeywordMapper.selectList(specialWrapper);
        log.info("评论列表：敏感词:[{}]", JSON.toJSONString(specialKeywords));
        Set<String> sensitiveWordSet = new HashSet<>();
        for (SpecialKeyword specialKeyword : specialKeywords) {
            sensitiveWordSet.add(specialKeyword.getSensitiveWords());
        }
        SensitiveWordsUtil sensitiveWordsUtil = new SensitiveWordsUtil();
        //初始化
        sensitiveWordsUtil.init(sensitiveWordSet);
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.eq("uid", userId);
        queryWrapper.eq("is_hide", 0);
        IPage<Comment> page = new Page<>(commentApiListRequest.getPageNum(), commentApiListRequest.getPageSize());
        page = commentMapper.selectPage(page, queryWrapper);
        log.info("评论列表：敏感词page:[{}]", JSON.toJSONString(page));
        List<Comment> commentPage = page.getRecords();
        log.info("评论列表：敏感词commentPage:[{}]", JSON.toJSONString(commentPage));
        if (commentPage.size() == 0) {
            throw new BaseException(CommentEnum.COMMENT_NEW_ONE.getCode(), CommentEnum.COMMENT_NEW_ONE.getMessage());
        }
        //有序列表
        List<CommentApiListResponse> result = new ArrayList<>();

        /* 根据商品ID--sku查询商品主图【商品服务提供接口】，提取商品id，调用商品接口返回商品信息列表*/
        List<Long> skuIds = new ArrayList<>();
        Integer skuUid = null;
        List<GoodsSkuInfoDTO> getGoodsSkuInfoByIds = new ArrayList<>();

        for (Comment comments : commentPage) {
            skuIds.add(comments.getGid().longValue());
            skuUid = comments.getUid().intValue();
        }
        log.info("评论列表：根据商品ID--sku查询商品主图【商品服务提供接口】skuIds:[{}]", JSON.toJSONString(skuIds));
        if (skuIds != null) {
            getGoodsSkuInfoByIds = goodsClient.getGoodsSkuInfoByIds(skuIds);
            log.info("评论列表：根据商品ID--sku查询商品主图【商品服务提供接口】:[{}]", JSON.toJSONString(getGoodsSkuInfoByIds));
        }
        //调用用户接口获取用户信息
        String userName = null;
        String userImage = null;
        if (skuUid != null) {
            /*用户的所有信息*/
            UserExternalResponse userExternalResponse = iUserExternalService.getUserByUid(skuUid);
            log.info("评论列表----【用户的所有信息】:[{}]", JSON.toJSONString(userExternalResponse));
            if (userExternalResponse != null) {
                userName = userExternalResponse.getNickName();
                userImage = userExternalResponse.getHeadUrl();
            }
        }


        //在下面方法中直接使用
        for (Comment comments : commentPage) {
            CommentApiListResponse commentApiListResponse = new CommentApiListResponse();
            ///敏感词替换为*
            String filterStr = sensitiveWordsUtil.replaceSensitiveWord(comments.getContent(), '*');
            comments.setContent(filterStr);
            BeanUtils.copyProperties(comments, commentApiListResponse);

            //设置商品名称.图片
            if (getGoodsSkuInfoByIds != null) {
                for (GoodsSkuInfoDTO getGoodsSkuIds : getGoodsSkuInfoByIds) {
                    if (getGoodsSkuIds.getId().equals(comments.getGid().longValue())) {
                        commentApiListResponse.setGidTitle(getGoodsSkuIds.getGoodsName());
                        commentApiListResponse.setGidImages(getGoodsSkuIds.getGoodsImage());
                    }
                }
            }

            //根据当前用户ID，获取用户相关信息
            commentApiListResponse.setUserName(userName);
            commentApiListResponse.setUserImage(userImage);
            commentApiListResponse.setCommentId(comments.getId());
            result.add(commentApiListResponse);
        }

        TableDataInfo<CommentApiListResponse> dataInfo = new TableDataInfo<>();
        dataInfo.setRows(result);
        dataInfo.setTotal(page.getTotal());
        return dataInfo;

    }

    /**
     * spu商品 评论列表
     *
     * @param commentSpuRequest
     * @return
     */
    public TableDataInfo<CommentApiListResponse> commentListSpuList(CommentSpuRequest commentSpuRequest) {
        Integer spuId = commentSpuRequest.getSpuGid();
        if (spuId == null) {
            throw new BaseException(CommentEnum.COMMENT_GId.getCode(), CommentEnum.COMMENT_GId.getMessage());
        }
        Integer types = commentSpuRequest.getCommentType();
        if (types == null) {
            types = 1;
        }
        /**敏感词*/
        QueryWrapper<SpecialKeyword> specialWrapper = new QueryWrapper<>();
        specialWrapper.orderByDesc("id");
        List<SpecialKeyword> specialKeywords = specialKeywordMapper.selectList(specialWrapper);
        log.info("spu商品评论列表----【敏感词】:[{}]", JSON.toJSONString(specialKeywords));
        Set<String> sensitiveWordSet = new HashSet<>();
        for (SpecialKeyword specialKeyword : specialKeywords) {
            sensitiveWordSet.add(specialKeyword.getSensitiveWords());
        }
        SensitiveWordsUtil sensitiveWordsUtil = new SensitiveWordsUtil();
        //初始化
        sensitiveWordsUtil.init(sensitiveWordSet);

        Byte isImages = commentSpuRequest.getIsImages();
        Byte isRepeatCustomers = commentSpuRequest.getIsRepeatCustomers();
        List<Integer> satisVals = new ArrayList<Integer>();
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.eq("spu_gid", spuId);
        queryWrapper.eq("is_hide", 0);
        switch (types) {
            case 2:
                satisVals.add(4);
                satisVals.add(5);
                queryWrapper.in("product_satisfaction", satisVals);
                break;
            case 3:
                satisVals.add(2);
                satisVals.add(3);
                queryWrapper.in("product_satisfaction", satisVals);
                break;
            case 4:
                satisVals.add(1);
                queryWrapper.in("product_satisfaction", satisVals);
                break;
            default:
                break;
        }
        if (isImages != null && isImages == 1) {
            queryWrapper.in("is_images", isImages);
        }
        if (isRepeatCustomers != null && isRepeatCustomers == 1) {
            queryWrapper.in("is_repeat_customers", isRepeatCustomers);
        }
        IPage<Comment> page = new Page<>(commentSpuRequest.getPageNum(), commentSpuRequest.getPageSize());
        page = commentMapper.selectPage(page, queryWrapper);
        List<Comment> commentPage = page.getRecords();
        log.info("spu商品评论列表----【commentPage】:[{}]", JSON.toJSONString(commentPage));
        if (commentPage.size() == 0) {
            throw new BaseException(CommentEnum.COMMENT_NEW_ONE.getCode(), CommentEnum.COMMENT_NEW_ONE.getMessage());
        }
        //根据用户uid查用户消息【用户服务提供接口】
        List<Integer> skuIds = new ArrayList<>();
        for (Comment comments : commentPage) {
            skuIds.add(comments.getUid().intValue());
        }
        R<List<JmUserReponse>> memberByUid = null;
        if (skuIds.size() > 0) {
            log.info("spu商品评论列表----【用户id】:[{}]", JSON.toJSONString(skuIds));
            memberByUid = iMemberService.getMemberByUids(skuIds);
            log.info("spu商品评论列表----【用户memberByUid】:[{}]", JSON.toJSONString(memberByUid));
        }
        List<JmUserReponse> memberByUidData = memberByUid.getData();

        //有序列表
        List<CommentApiListResponse> result = new ArrayList<>();

        for (Comment comments : commentPage) {
            ///敏感词替换为*
            String filterStr = sensitiveWordsUtil.replaceSensitiveWord(comments.getContent(), '*');
            comments.setContent(filterStr);

            CommentApiListResponse commentApiListResponse = new CommentApiListResponse();
            BeanUtils.copyProperties(comments, commentApiListResponse);
            //根据当前用户ID，获取用户相关信息
            //设置用户信息
            if (memberByUidData.size() > 0) {
                commentApiListResponse.setUserName(memberByUidData.iterator().next().getName());
                commentApiListResponse.setUserImage(memberByUidData.iterator().next().getAvatar());
            }
            commentApiListResponse.setCommentId(comments.getId());
            result.add(commentApiListResponse);
        }
        TableDataInfo<CommentApiListResponse> dataInfo = new TableDataInfo<>();
        dataInfo.setRows(result);
        dataInfo.setTotal(page.getTotal());
        return dataInfo;
    }

    /**
     * 商品评论列表orderid
     *
     * @param commentApiUidRequest
     * @return
     */
    public TableDataInfo<CommentApiUidResponse> commentListUid(CommentApiUidRequest commentApiUidRequest) {
        Long uid = commentApiUidRequest.getUid();
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        queryWrapper.orderByDesc("id");
        List<Comment> comment = commentMapper.selectList(queryWrapper);
        log.info("商品评论列表orderid----【comment】:[{}]", JSON.toJSONString(comment));
        if (comment.size() == 0) {
            throw new BaseException(CommentEnum.COMMENT_NEW_ONE.getCode(), CommentEnum.COMMENT_NEW_ONE.getMessage());
        }
        List<CommentApiUidResponse> result = new ArrayList<>();
        for (Comment comment3 : comment) {
            CommentApiUidResponse commentApiUidResponse = new CommentApiUidResponse();
            BeanUtils.copyProperties(comment3, commentApiUidResponse);
            result.add(commentApiUidResponse);
        }
        TableDataInfo<CommentApiUidResponse> dataInfo = new TableDataInfo<>();
        dataInfo.setRows(result);
        return dataInfo;
    }

    /**
     * 评论批量添加
     *
     * @param commentApiAddBatchRequest
     * @return
     */
    public boolean addBatchComment(CommentApiAddBatchRequest commentApiAddBatchRequest) {
        Long userId = commentApiAddBatchRequest.getUid();
        if (userId == null) {
            throw new BaseException(CommentEnum.NO_LOGIN.getCode(), CommentEnum.NO_LOGIN.getMessage());
        }
        List<Comment> commentList = new ArrayList<>();
        List<CommentApiGoodsRequest> listArr = commentApiAddBatchRequest.getGoodsList();
        log.info("评论批量添加----【listArr】:[{}]", JSON.toJSONString(listArr));
        for (CommentApiGoodsRequest commentApiGoodsRequest : listArr) {
            Comment comment = new Comment();
            //条件判断
            Integer gid = commentApiGoodsRequest.getGid();
            if (gid == null) {
                throw new BaseException(CommentEnum.COMMENT_GId.getCode(), CommentEnum.COMMENT_GId.getMessage());
            }
            String content = commentApiGoodsRequest.getContent().trim();
            //字数限制
            if (content.length() > 200) {
                throw new BaseException(CommentEnum.COMMENT_LENGTH_LONG.getCode(), CommentEnum.COMMENT_LENGTH_LONG.getMessage());
            }
            if (content == null || content.equals("")) {
                //评论为空，默认文案并好评
                commentApiGoodsRequest.setContent("用户默认好评！");
            }
            if (commentApiAddBatchRequest.getExpressPackaging() == null) {
                commentApiAddBatchRequest.setExpressPackaging(5);
            }
            if (commentApiAddBatchRequest.getLogisticsServices() == null) {
                commentApiAddBatchRequest.setLogisticsServices(5);
            }
            if (commentApiAddBatchRequest.getServiceAttitude() == null) {
                commentApiAddBatchRequest.setServiceAttitude(5);
            }
            Integer productSatisfaction = commentApiGoodsRequest.getProductSatisfaction();
            if (productSatisfaction == null || productSatisfaction.equals("")) {
                commentApiGoodsRequest.setProductSatisfaction(5);
            }
            Integer spuGid = commentApiGoodsRequest.getSpuGid();
            if (spuGid == null || spuGid.equals("")) {
                throw new BaseException(CommentEnum.COMMENT_SPUGID.getCode(), CommentEnum.COMMENT_SPUGID.getMessage());
            }
            comment.setSpuGid(spuGid);
            String images = commentApiGoodsRequest.getImages();
            if (images != null) {
                //图片以；分割
                String[] idArr = images.split(";");
                if (idArr.length > 6) {
                    throw new BaseException(CommentEnum.COMMENT_IMAGES_LONG.getCode(), CommentEnum.COMMENT_IMAGES_LONG.getMessage());
                }
                if (idArr.length > 0) {
                    comment.setIsImages((byte) 1);
                }
            } else {
                comment.setIsImages((byte) 0);
            }
            Integer orderId = commentApiGoodsRequest.getOrderId();
            if (orderId == null) {
                orderId = 0;
            }
            comment.setOrderId(orderId);

            /*查询spugid  uid是否有数据如果有设置为1 否则为0，并把已有数据更新1*/
            QueryWrapper<Comment> spuWrapper = new QueryWrapper<>();
            spuWrapper.eq("spu_gid", spuGid);
            spuWrapper.eq("uid", userId);
            spuWrapper.eq("is_hide", 0);
            Integer commentsList = commentMapper.selectCount(spuWrapper);
            log.info("评论批量添加----【commentsList】:[{}]", JSON.toJSONString(commentsList));
            if (commentsList > 0) {
                //回头客
                comment.setIsRepeatCustomers((byte) 1);
                Integer integer = commentMapper.repeatUpdata(spuGid);
                log.info("评论批量添加----【integer】:[{}]", JSON.toJSONString(integer));
                if (integer == 0) {
                    throw new BaseException(CommentEnum.COMMENT_REPEAT_ERROR.getCode(), CommentEnum.COMMENT_REPEAT_ERROR.getMessage());
                }
            } else {
                comment.setIsRepeatCustomers((byte) 0);
            }
            Byte isAnonymous = commentApiAddBatchRequest.getIsAnonymous();
            if (isAnonymous == null) {
                isAnonymous = (byte) 0;
            }

            //用户uid
            comment.setUid(commentApiAddBatchRequest.getUid());
            comment.setUserName(commentApiAddBatchRequest.getUserName());
            //商品信息集合
            comment.setGid(commentApiGoodsRequest.getGid());
            comment.setContent(commentApiGoodsRequest.getContent());
            comment.setImages(commentApiGoodsRequest.getImages());
            comment.setProductSatisfaction(commentApiGoodsRequest.getProductSatisfaction());
            //其他共用信息
            comment.setExpressPackaging(commentApiAddBatchRequest.getExpressPackaging());
            comment.setLogisticsServices(commentApiAddBatchRequest.getLogisticsServices());
            comment.setServiceAttitude(commentApiAddBatchRequest.getServiceAttitude());
            comment.setIsAnonymous(isAnonymous);
            commentList.add(comment);
        }
        log.info("评论批量添加----【commentList】:[{}]", JSON.toJSONString(commentList));
        return commentMapper.batchInsertComment(commentList) > 0;
    }

    /**
     * 最新一条评论
     *
     * @param commentApiOneRequest
     * @return
     */
    public CommentApiOneResponse commentNewOned(CommentApiOneRequest commentApiOneRequest) {
        Integer gId = commentApiOneRequest.getSpuGid();
        if (gId == null) {
            throw new BaseException(CommentEnum.COMMENT_GId.getCode(), CommentEnum.COMMENT_GId.getMessage());
        }
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.eq("spu_gid", gId);
        /*取出最新一条数据*/
        queryWrapper.last("limit 1");
        Comment commentone = commentMapper.selectOne(queryWrapper);
        log.info("最新一条评论----【取出最新一条数据】:[{}]", JSON.toJSONString(commentone));
        if (commentone == null) {
            throw new BaseException(CommentEnum.COMMENT_NEW_ONE.getCode(), CommentEnum.COMMENT_NEW_ONE.getMessage());
        }
        //全部数量
        QueryWrapper<Comment> queryCount = new QueryWrapper<>();
        queryCount.eq("spu_gid", gId);
        queryCount.eq("is_hide", 0);
        Integer commentAllNum = commentMapper.selectCount(queryCount);
        log.info("最新一条评论----【全部数量】:[{}]", JSON.toJSONString(commentAllNum));
        if (commentAllNum == null) {
            commentAllNum = 0;
        }
        //好评数
        List<Integer> satisVals = new ArrayList<Integer>();
        satisVals.add(4);
        satisVals.add(5);
        QueryWrapper<Comment> queryWrapperHigh = new QueryWrapper<>();
        queryWrapperHigh.eq("spu_gid", gId);
        queryWrapperHigh.in("product_satisfaction", satisVals);
        queryWrapperHigh.eq("is_hide", 0);
        Integer commentHighNum = commentMapper.selectCount(queryWrapperHigh);
        log.info("最新一条评论----【好评数】:[{}]", JSON.toJSONString(commentHighNum));
        if (commentHighNum == null) {
            commentHighNum = 0;
        }

        //好评率
        String highPraiseRate = "0.00%";
        if (commentHighNum == null) {
            highPraiseRate = "0.00%";
        }
        if (commentAllNum != 0 && commentHighNum != 0) {
            //定义格式化起始位数
            String format = "0.00";
            DecimalFormat dec = new DecimalFormat(format);
            highPraiseRate = dec.format((double) commentHighNum / commentAllNum * 100) + "%";
        }
        //用户姓名头像信息
        Integer uid = commentone.getUid().intValue();
        R<JmUserReponse> memberByUid = null;
        UserExternalResponse userExternalResponse = new UserExternalResponse();
        if (uid != null && uid != 0) {
            log.info("最新一条评论----【用户姓名头像信息uid】:[{}]", JSON.toJSONString(uid));
            userExternalResponse = iUserExternalService.getUserByUid(uid);
            log.info("最新一条评论----【用户姓名头像信息】:[{}]", JSON.toJSONString(userExternalResponse));
        }

        //传递信息
        CommentApiOneResponse commentApiOneResponse = new CommentApiOneResponse();
        BeanUtils.copyProperties(commentone, commentApiOneResponse);
        commentApiOneResponse.setCommentAllNum(commentAllNum);
        commentApiOneResponse.setCommentHighNum(highPraiseRate);
        if (userExternalResponse != null) {
            commentApiOneResponse.setUserName(userExternalResponse.getNickName());
            commentApiOneResponse.setUserAvatar(userExternalResponse.getHeadUrl());
        }
        commentApiOneResponse.setUid(commentone.getUid());
        commentApiOneResponse.setCreateTime(commentone.getCreateTime());
        return commentApiOneResponse;
    }

    /**
     * spu商品所有评价顶部评论数量一级搜索
     *
     * @param commentSpuGidRequest
     * @return
     */
    public CommentSpuGidResponse commentGidNum(CommentSpuGidRequest commentSpuGidRequest) {
        Integer spuGid = commentSpuGidRequest.getSpuGid();
        if (spuGid == null) {
            throw new BaseException(CommentEnum.COMMENT_GId.getCode(), CommentEnum.COMMENT_GId.getMessage());
        }
        CommentSpuGidResponse commentSpuGidResponse = new CommentSpuGidResponse();
        //全部数量
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_gid", spuGid);
        queryWrapper.eq("is_hide", 0);
        Integer commentAllNum = commentMapper.selectCount(queryWrapper);
        log.info("spu商品所有评价顶部评论数量一级搜索----【全部数量】:[{}]", JSON.toJSONString(commentAllNum));
        if (commentAllNum == null) {
            commentAllNum = 0;
        }
        commentSpuGidResponse.setAllNum(commentAllNum);
        //好评数
        List<Integer> satisVals = new ArrayList<Integer>();
        satisVals.add(4);
        satisVals.add(5);
        QueryWrapper<Comment> queryWrapperHigh = new QueryWrapper<>();
        queryWrapperHigh.eq("spu_gid", spuGid);
        queryWrapperHigh.in("product_satisfaction", satisVals);
        queryWrapperHigh.eq("is_hide", 0);
        Integer commentHighNum = commentMapper.selectCount(queryWrapperHigh);
        log.info("spu商品所有评价顶部评论数量一级搜索----【好评数】:[{}]", JSON.toJSONString(commentHighNum));
        if (commentHighNum == null) {
            commentHighNum = 0;
        }
        commentSpuGidResponse.setPraiseNum(commentHighNum);
        //中评数
        List<Integer> satisAverage = new ArrayList<Integer>();
        satisAverage.add(2);
        satisAverage.add(3);
        QueryWrapper<Comment> queryAverageNum = new QueryWrapper<>();
        queryAverageNum.eq("spu_gid", spuGid);
        queryAverageNum.in("product_satisfaction", satisAverage);
        queryAverageNum.eq("is_hide", 0);
        Integer commentAverageNum = commentMapper.selectCount(queryAverageNum);
        log.info("spu商品所有评价顶部评论数量一级搜索----【中评数】:[{}]", JSON.toJSONString(commentAverageNum));
        if (commentAverageNum == null) {
            commentAverageNum = 0;
        }
        commentSpuGidResponse.setAverageNum(commentAverageNum);
        //查评数
        QueryWrapper<Comment> queryNegativeNum = new QueryWrapper<>();
        queryNegativeNum.eq("spu_gid", spuGid);
        queryNegativeNum.in("product_satisfaction", 1);
        queryNegativeNum.eq("is_hide", 0);
        Integer commentNegativeNum = commentMapper.selectCount(queryNegativeNum);
        log.info("spu商品所有评价顶部评论数量一级搜索----【查评数】:[{}]", JSON.toJSONString(commentNegativeNum));
        if (commentNegativeNum == null) {
            commentNegativeNum = 0;
        }
        commentSpuGidResponse.setNegativeNum(commentNegativeNum);

        //满意度
        //好评率
        String highPraiseRate = "0.00%";
        if (commentHighNum == null) {
            highPraiseRate = "0.00%";
        }
        String format1 = null;
        Integer satisfaction = 0;
        if (commentAllNum != 0 && commentHighNum != 0) {
            //定义格式化起始位数
            String format = "0.00";
            DecimalFormat dec = new DecimalFormat(format);
            format1 = dec.format((double) commentHighNum / commentAllNum * 100);
            if (format1.compareTo("20") <= 0) {
                satisfaction = 1;
            } else if (format1.compareTo("40") <= 0 && format1.compareTo("20") > 0) {
                satisfaction = 2;
            } else if (format1.compareTo("60") <= 0 && format1.compareTo("40") > 0) {
                satisfaction = 3;
            } else if (format1.compareTo("80") < 0 && format1.compareTo("60") > 0) {
                satisfaction = 4;
            } else {
                satisfaction = 5;
            }
            highPraiseRate = dec.format((double) commentHighNum / commentAllNum * 100) + "%";
        }
        commentSpuGidResponse.setSatisfaction(satisfaction);
        commentSpuGidResponse.setHighPraiseRate(highPraiseRate);

        return commentSpuGidResponse;

    }

    /**
     * spu商品所有评价顶部评论数量
     * 二级搜索商品评价图片回头客数量
     *
     * @param commentSpuImageNumRequest
     * @return
     */
    public CommentSpuImageNumResponse commentGidTwoNum(CommentSpuImageNumRequest commentSpuImageNumRequest) {
        Map<String, Integer> map = new HashMap<>();
        Integer spuGid = commentSpuImageNumRequest.getSpuGid();
        if (spuGid == null) {
            throw new BaseException(CommentEnum.COMMENT_GId.getCode(), CommentEnum.COMMENT_GId.getMessage());
        }
        Integer commentType = commentSpuImageNumRequest.getCommentType();
        if (commentType == null || commentType.equals("")) {
            throw new BaseException(CommentEnum.COMMENT_SEARCH_TYPE.getCode(), CommentEnum.COMMENT_SEARCH_TYPE.getMessage());
        }
        map.put("spuGid", spuGid);
        map.put("commentType", commentType);
        Integer allNums = this.allNum(map);
        Integer imageNum = this.imageNum(map);
        Integer isRepeatNum = this.isRepeatNum(map);
        CommentSpuImageNumResponse commentSpuImageNumResponse = new CommentSpuImageNumResponse();
        commentSpuImageNumResponse.setAllNum(allNums);
        commentSpuImageNumResponse.setIsImagesNum(imageNum);
        commentSpuImageNumResponse.setIsRepeatCustomersNum(isRepeatNum);
        commentSpuImageNumResponse.setCommentType(commentType);
        commentSpuImageNumResponse.setSpuGid(spuGid);
        return commentSpuImageNumResponse;
    }

    /*全部的数量*/
    private Integer allNum(Map<String, Integer> map) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_gid", map.get("spuGid"));
        List<Integer> satisVals = new ArrayList<Integer>();

        switch (map.get("commentType")) {
            case 2:
                satisVals.add(4);
                satisVals.add(5);
                queryWrapper.in("product_satisfaction", satisVals);
                break;
            case 3:
                satisVals.add(2);
                satisVals.add(3);
                queryWrapper.in("product_satisfaction", satisVals);
                break;
            case 4:
                satisVals.add(1);
                queryWrapper.in("product_satisfaction", satisVals);
                break;
            default:
                break;
        }
        queryWrapper.eq("is_hide", 0);
        Integer commentAllNum = commentMapper.selectCount(queryWrapper);
        log.info("全部的数量:[{}]", JSON.toJSONString(commentAllNum));
        if (commentAllNum == null) {
            commentAllNum = 0;
        }
        return commentAllNum;
    }

    /**
     * 有图片的数量
     *
     * @param map
     * @return
     */
    private Integer imageNum(Map<String, Integer> map) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_gid", map.get("spuGid"));
        List<Integer> satisVals = new ArrayList<Integer>();

        switch (map.get("commentType")) {
            case 2:
                satisVals.add(4);
                satisVals.add(5);
                queryWrapper.in("product_satisfaction", satisVals);
                break;
            case 3:
                satisVals.add(2);
                satisVals.add(3);
                queryWrapper.in("product_satisfaction", satisVals);
                break;
            case 4:
                satisVals.add(1);
                queryWrapper.in("product_satisfaction", satisVals);
                break;
            default:
                break;
        }
        queryWrapper.in("is_images", 1);
        queryWrapper.eq("is_hide", 0);
        Integer commentAllNum = commentMapper.selectCount(queryWrapper);
        log.info("有图片的数量:[{}]", JSON.toJSONString(commentAllNum));
        if (commentAllNum == null) {
            commentAllNum = 0;
        }
        return commentAllNum;
    }

    /**
     * 回头客的数量
     *
     * @param map
     * @return
     */
    private Integer isRepeatNum(Map<String, Integer> map) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_gid", map.get("spuGid"));
        List<Integer> satisVals = new ArrayList<Integer>();

        switch (map.get("commentType")) {
            case 2:
                satisVals.add(4);
                satisVals.add(5);
                queryWrapper.in("product_satisfaction", satisVals);
                break;
            case 3:
                satisVals.add(2);
                satisVals.add(3);
                queryWrapper.in("product_satisfaction", satisVals);
                break;
            case 4:
                satisVals.add(1);
                queryWrapper.in("product_satisfaction", satisVals);
                break;
            default:
                break;
        }
        queryWrapper.in("is_repeat_customers", 1);
        queryWrapper.eq("is_hide", 0);
        Integer commentAllNum = commentMapper.selectCount(queryWrapper);
        log.info("回头客的数量:[{}]", JSON.toJSONString(commentAllNum));
        if (commentAllNum == null) {
            commentAllNum = 0;
        }
        return commentAllNum;
    }
}
